import axios from '../js/httpRequestTool.js';

const defaultMenu = 
[
/*
  {
    menuType: 'item',
    iconClass: 'fa fa-home',
    name: '主页',
    routerName: 'homePage'
  },
  {
    menuType: 'tree',
    iconClass: 'fa fa-navicon',
    name: '健康评估管理',
    routerName: 'healthPersonnel',
    subMenu:[
      {
        menuType: 'item',
        iconClass: 'fa fa-vcard',
        name: '老年人信息',
        routerName: 'healthPersonnel'
      }
      {
        menuType: 'item',
        iconClass: 'fa fa-file-archive-o',
        name: '新建评估调查',
        routerName: 'newAssessment'
      }
    ]
  },
*/
  {
    menuType: 'tree',
    iconClass: 'fa fa-navicon',
    name: '机构评估管理',
    routerName: 'orgKpi',
    subMenu:[
      {
        menuType: 'item',
        iconClass: 'fa fa-vcard',
        name: '机构信息',
        routerName: 'orgKpi'
      }
    ]
  },
  {
    menuType: 'tree',
    iconClass: 'fa fa-navicon',
    name: '产品题库管理',
    routerName: 'questionnaire',
    subMenu: [
      {
        menuType: 'item',
        iconClass: 'fa fa-file-text-o',
        name: '产品管理',
        routerName: 'questionnaire'
      },
      {
        menuType: 'item',
        iconClass: 'fa fa-question-circle-o',
        name: '题库管理',
        routerName: 'questions'
      },
      {
        menuType: 'item',
        iconClass: 'fa fa-tree',
        name: '分类管理',
        routerName: 'questionCategory'
      }
    ]
  },
  {
    menuType: 'tree',
    iconClass: 'fa fa-cog',
    name: '系统管理',
    routerName: 'systemUser',
    subMenu:[
      {
        menuType: 'item',
        iconClass: 'fa fa-user',
        name: '用户管理',
        routerName: 'systemUser'
      },
      {
        menuType: 'item',
        iconClass: 'fa fa-universal-access',
        name: '角色管理',
        routerName: 'systemRole'
      },
      /*
      {
        menuType: 'item',
        iconClass: 'fa fa-file',
        name: '分组管理',
        routerName: 'organization'
      }
      {
        menuType: 'item',
        iconClass: 'fa fa-file',
        name: '问卷权限分配',
        routerName: ''
      }*/
    ]
  }
];
const orgMenu = 
[
/*
  {
    menuType: 'item',
    iconClass: 'fa fa-home',
    name: '主页',
    routerName: 'homePage'
  },
  {
    menuType: 'tree',
    iconClass: 'fa fa-navicon',
    name: '健康评估管理',
    routerName: 'healthPersonnel',
    subMenu:[
      {
        menuType: 'item',
        iconClass: 'fa fa-vcard',
        name: '被评估人员',
        routerName: 'healthPersonnel'
      }
      {
        menuType: 'item',
        iconClass: 'fa fa-file-archive-o',
        name: '新建评估调查',
        routerName: 'newAssessment'
      }
    ]
  },
  */
   {
    menuType: 'tree',
    iconClass: 'fa fa-navicon',
    name: '机构评估管理',
    routerName: 'orgKpi',
    subMenu:[
      {
        menuType: 'item',
        iconClass: 'fa fa-vcard',
        name: '机构信息',
        routerName: 'orgKpi'
      }
    ]
  }
];
const appraiserMenu = 
[
/*
  {
    menuType: 'tree',
    iconClass: 'fa fa-navicon',
    name: '健康评估管理',
    routerName: 'healthPersonnel',
    subMenu:[
      {
        menuType: 'item',
        iconClass: 'fa fa-vcard',
        name: '被评估人员',
        routerName: 'healthPersonnel'
      }
    ]
  }
  */
  {
    menuType: 'tree',
    iconClass: 'fa fa-navicon',
    name: '机构评估管理',
    routerName: 'orgKpi',
    subMenu:[
      {
        menuType: 'item',
        iconClass: 'fa fa-vcard',
        name: '机构信息',
        routerName: 'orgKpi'
      }
    ]
  }
];
const modelUser = {
  state: {
    isLogin: false,
    userData: {
      id: null,
      username: null,
      userType: null,
      organizationId: null,
      organizationName: null
//      domainName: null,
//      logo: './static/images/logo.jpg'
    },
    // 菜单配置
    itemData: []
  },
  getters:{
    getLoginState: state => {
      return state.isLogin;
    },
    getUserData: state => {
      return state.userData;
    },
    getNav: state => {
      return state.itemData;
    }
  },
  mutations: {
    setLoginState (state, _boole) {
      state.isLogin = _boole;
    },
    // 设置登录用户数据
    setUserData (state, data) {
      if (data === null) {
        data = {
          id: null,
          username: null,
          userType: null,
          organizationId: null,
          organizationName: null,
          domainName: null,
          logo: null
        };
      }
      state.userData = data;
      localStorage.setItem("userData", JSON.stringify(data));
    },
    // 设置菜单
    setNav (state, navItem) {
      state.itemData = navItem;
    },
    // 根据用户类型设置访问菜单
    setNavByUserType (_state, userType) {
      if (userType === 'ADMIN') {
        _state.itemData = defaultMenu;
      }
      else if (userType === 'ORGADMIN') {
         _state.itemData = orgMenu;
      }
      else if (userType === 'APPRAISER') {
        _state.itemData = appraiserMenu;
      }
      else {
        _state.itemData = [];
      }
    }
  },
  // Action 方法通过 store.dispatch('方法名'，参数) 触发：
  actions: {
    login ({ dispatch,commit, state }, loginData) {
      axios.post('login', loginData).then(
        response => {
          if (response.status === 200) {
            commit('setUserData', response.data.data);
            // 设置菜单
            // dispatch('getNavByAuthor',state.userData..id);
            commit('setNavByUserType', state.userData.userType);
            
            commit('setLoginState', true);
          }
          return response;
        }
      );
    },
    logout ({ commit }) {
      axios.get('logout').then(
        response => {
          if (response.status === 200 || response.status === 204 || response.status === 302) {
            commit('setUserData', null);
            commit('setLoginState', false);
            window.location.href = './';
          }
        }
      );
    },
    checkLoginState ({ dispatch,commit, state }) {
      // headers->Pragma->no-cache 禁用请求缓存
      axios.get('checkIsLogin',{headers:{'Pragma':'no-cache'}}).catch(function (error) {
        commit('setLoginState', false);
        return false;
      }).then(
        response => {
          if (response.status === 200) {
            if (response.data.data) {
              // 刷新页面时需要重新设置已登录用户数据
              if (!state.userData.id) {
                let userData = JSON.parse(localStorage.getItem("userData"));
                commit('setUserData', userData);
              }
            }else{
              commit('setUserData', null);
            }
            commit('setLoginState', response.data.data);
          }
          // 设置菜单
          // dispatch('getNavByAuthor',state.userData.id);
          commit('setNavByUserType', state.userData.userType);
          return true;
        }
      );
    },
    // 通过系统权限获取菜单 未实现
    getNavByAuthor ({ commit }, userId) {
      axios.get('./userNav?userId=' + userId).then(
        resp => {
          commit('setNav', resp.data.data);
          return resp.data.data;
        }
      );
    }
  }
};

export default modelUser;
